/**
 * main.cs
 * main javascript file
 * Author : Minkyu Shim
 */

var uid; //current user's id
		
//This is a entry point of FB SDK
window.fbAsyncInit = function(){
	
	//Initialising
	FB.init({
		appId: '[##_FB_APP_ID_##]',
		status: true, // check login status
		cookie: true, // enable cookies to allow the server to access the session
		xfbml: true // parse XFBML
	});

	//check if the user logged in Facebook
	FB.getLoginStatus(function(response){
		
		if(response.session){
			uid = response.session.uid;
			showLogoutButton();
			if(response.perms){
				
				//show a loading image
				loading();
				
				///////////////////////////////////////////////////////
				// Execute queries and get data
				///////////////////////////////////////////////////////
				// get friends data
				
				getPostByFriendsWithGraphAPI();
				getPostByFriendsWithFQL();
							
			}
			
		} else {
			//if there is no valid session, show login button
			showLoginButton();
		}
		
	});
	
	
	FB.Event.subscribe('auth.login', function(){
		window.location.reload();
	});
	
	FB.Event.subscribe('auth.logout', function(){
		window.location.reload();
	});
	
};

function showLoginButton(){
	$('<fb:login-button perms="read_stream"></fb:login-button>').appendTo('#button-container');
	FB.XFBML.parse();
}

function showLogoutButton(){
	$('<input type="button" value="logout"/>').click(function(){
		FB.logout();
	}).appendTo('#button-container');
}

function getPostByFriendsWithGraphAPI(){
				FB.api("/me/feed?fields=id,from,created_time&limit=100", function(response){
				    //get feed written by friends
				    var feed = response.data;
					var count = 0;
					//filter user's own posts
					feed = $.grep(feed, function(item){
						var flag;
						if(item.from.id == uid){
							flag = false;

						}else{
							flag = true;
							count++;
						}
						return flag;
					});
					
					var markup = 	"<li class=\"friend\">" +
									"	<div class=\"clearfix\">" + 
									"		<a href=\"#\">" + 
									"			<img src=\"http://graph.facebook.com/${from.id}/picture\" />" + 
									"		</a>"+
									"		<div class=\"content\">" + 
									"			<a href=\"http://graph.facebook.com/${id}\">" + 
									"				${from.name}" + 
									"			</a> wrote something on your wall on ${created_time}." + 
									"		</div>" + 
									"	</div>" + 
									"</li>";
					
					$.template("friendsTemplate", markup); 
					$.tmpl("friendsTemplate", feed).appendTo('#friend-list');
					
					
					console.log(count);
					//remove the loading image
					loaded();
				    
				});		
}

function getPostByFriendsWithFQL(){
	FB.api(
		{
		method: 'fql.query',
		query : "SELECT post_id, actor_id, message, created_time FROM stream WHERE actor_id != me() and source_id = me() LIMIT 53"
		},
		function(response){
			console.log(response.length);
			var markup = 	"<li class=\"friend\">" +
							"	<div class=\"clearfix\">" + 
							"		<a href=\"#\">" + 
							"			<img src=\"http://graph.facebook.com/${actor_id}/picture\" />" + 
							"		</a>"+
							"		<div class=\"content friendMessage \">" + 
							"			<a href=\"http://graph.facebook.com/${actor_id}\">" + 
							"				" + 
							"			</a>${message} on ${created_time}" + 
							"		</div>" + 
							"	</div>" + 
							"</li>";
			
			$.template("friendsTemplate", markup); 
			$.tmpl("friendsTemplate", response).appendTo('#friend-list');
			
			loaded();
			
		}
	);
}
	